﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business.Compta
{
    [System.Diagnostics.DebuggerDisplay("IdUser ={idUser}, IsComptaLineSup={isComptaLineSup}, Date={date}")]
    public class ComptaLine : ManageoNotifyPropertyChanged
    {
        protected int id;
        private int idAccount;
        private DateTime date, _date;
        private DateTime dateMonth;
        private DateTime insertDate, _insertDate;
        private DateTime modificationDate, _modificationDate;
        protected double amount;
        private double _amount;
        protected Category category;
        private Category _category;
        protected int idUser;
        private int _idUser;
        protected String comment;
        private String _comment;
        private double credit, debit;
        protected bool isChecked;
        private bool _isChecked;
        protected bool isPermanent;
        private bool _isPermanent;
        protected bool isComptaLineSup;
        private bool _isComptaLineSup;

        public int Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
                //onPropertyChanged("Id");
            }
        }

        public bool IsChecked
        {
            get
            {
                return isChecked;
            }
            set
            {
                isChecked = value;
                //onPropertyChanged("IsChecked");
            }
        }
        public bool IsPermanent
        {
            get
            {
                return isPermanent;
            }
            set
            {
                isPermanent = value;
                //onPropertyChanged("IsPermanent");
            }
        }
        public DateTime Month
        {
            get
            {
                return dateMonth;
            }
        }

        public int IdAccount
        {
            get
            {
                return idAccount;
            }
            set
            {
                idAccount = value;
                //onPropertyChanged("IdAccount");
            }
        }

        public DateTime Date
        {
            get
            {
                return date;
            }
            set
            {
                date = value;
                dateMonth = new DateTime(date.Year, date.Month, 1);
                //onPropertyChanged("Date");
            }
        }

        public Category Category
        {
            get
            {
                return category;
            }
            set
            {
                this.category = value;
                //onPropertyChanged("Category");
            }
        }

        public int IdUser
        {
            get
            {
                return idUser;
            }
            set
            {
                this.idUser = value;
                //onPropertyChanged("IdUser");
            }
        }

        public bool IsComptaLineSup
        {
            get
            {
                return this.isComptaLineSup;
            }
            set
            {
                this.isComptaLineSup = value;
            }
        }

        public virtual double Amount
        {
            get
            {
                if (!isComptaLineSup)
                    return amount;
                else return 0;
            }
            set
            {
                this.amount = value;
                if (amount > 0)
                {
                    debit = 0;
                    credit = amount;
                }
                else
                {
                    credit = 0;
                    debit = Math.Abs(amount);
                }
                //onPropertyChanged("Amount");
            }
        }
        public double Credit
        {
            get
            {
                return credit;
            }
        }
        public double Debit
        {
            get
            {
                return debit;
            }
        }

        public String Comment
        {
            get
            {
                return comment;
            }
            set
            {
                this.comment = value;
                //onPropertyChanged("Comment");
            }
        }

        public DateTime InsertDate
        {
            get
            {
                return insertDate;
            }
            set
            {
                insertDate = value; ;
                //onPropertyChanged("InsertDate");
            }
        }

        public DateTime ModificationDate
        {
            get
            {
                return modificationDate;
            }
            set
            {
                modificationDate = value; ;
                //onPropertyChanged("ModificationDate");
            }
        }

        public ComptaLine()
        {
            this.id = -1;
            this.idAccount = -1;
            this.Amount = 0;
            this.category = null;
            this.idUser = -1;
            this.comment = null;

            this.Date = DateTime.Parse(DateTime.Now.ToShortDateString());
            //this.dateMonth = new DateTime(date.Year, date.Month, 1);
            //this.dateMonth = new DateTime(date.Year, date.Month, 1);
            this.isChecked = true;
            this.isPermanent = true;
            this.isComptaLineSup = false;            
            
            //this.insertDate = DateTime.Now;
            //this.modificationDate = DateTime.Now;
        }

        public ComptaLine(ComptaLine comptaLine)
        {
            this.id = comptaLine.Id;
            this.idAccount = comptaLine.idAccount;
            this.Amount = comptaLine.Amount;
            this.category = comptaLine.Category;
            this.idUser = comptaLine.IdUser;
            this.comment = comptaLine.Comment;
            this.Date = comptaLine.Date;
            //this.dateMonth = new DateTime(date.Year, date.Month, 1);
            this.isChecked = comptaLine.IsChecked;
            this.isPermanent = comptaLine.IsPermanent;
            this.isComptaLineSup = comptaLine.IsComptaLineSup;
        }

        public ComptaLine(int _id, int _idAccount, DateTime _date, int _user, double _amount, String _comment, Category _category, DateTime _insertDate, DateTime _modificationDate, bool _isChecked, bool _isPermanent, bool _isComptaLineSup)
        {
            this.id = _id;
            this.idAccount = _idAccount;
            this.Amount = _amount;
            this.category = _category;
            this.comment = _comment;
            this.Date = _date;
            //this.dateMonth = new DateTime(date.Year, date.Month, 1);
            this.insertDate = _insertDate;
            this.modificationDate = _modificationDate;
            this.idUser = _user;
            this.isChecked = _isChecked;
            this.isPermanent = _isPermanent;
            this.isComptaLineSup = _isComptaLineSup;

            this._amount = this.Amount;
            this._category = this.category;
            this._comment = this.comment;
            this._date = this.date;
            this._insertDate = this.insertDate;
            this._modificationDate = this.modificationDate;
            this._idUser = this.idUser;
            this._isChecked = this.isChecked;
            this._isPermanent = this.isPermanent;
            this._isComptaLineSup = this.isComptaLineSup;
        }

        public virtual void commit()
        {
            this._amount = this.Amount;
            this._category = this.category;
            this._comment = this.comment;
            this._date = this.date;
            this._insertDate = this.insertDate;
            this._modificationDate = this.modificationDate;
            this._idUser = this.idUser;
            this._isChecked = this.isChecked;
            this._isPermanent = this.isPermanent;
            this._isComptaLineSup = this.isComptaLineSup;
        }

        public virtual void reject()
        {
            this.isPermanent = this._isPermanent;
            this.Amount = this._amount;
            this.category = this._category;
            this.comment = this._comment;
            this.Date = this._date;
            //this.dateMonth = new DateTime(date.Year, date.Month, 1);
            this.insertDate = this._insertDate;
            this.modificationDate = this._modificationDate;
            this.idUser = this._idUser;
            this.isChecked = this._isChecked;
            this.isComptaLineSup = this._isComptaLineSup;
        }

        public override String ToString()
        {
            return this.Id + " - " + this.IsPermanent;
        }
    }
    
    public class ComptaLineSup : ComptaLine
    {
        private int idLine, _idLine;
        private int _idAccount;

        public int IdLine
        {
            get
            {
                return this.idLine;
            }
            set
            {
                this.idLine = value;
                //onPropertyChanged("IdLine");
            }
        }

        public ComptaLineSup()
        {
            this.Id = -1;
        }

        public ComptaLineSup(int _id, int _idLine, int _idAccount, DateTime _date, int _idUser, double _amount, string _comment, Category _category, DateTime _insertDate, DateTime _modificationDate)
            : base(_id, _idAccount, _date, _idUser, _amount, _comment, _category, _insertDate, _modificationDate,true, false, true)
        {
            this.idLine = _idLine;
            this._idLine = _idLine;
            this._idAccount = _idAccount;
        }

        public ComptaLineSup(ComptaLineSup ls)
            : base(ls.Id, ls.IdAccount, ls.Date, ls.IdUser, ls.Amount, ls.Comment, ls.Category, ls.InsertDate, ls.ModificationDate, ls.IsChecked, ls.IsPermanent, ls.IsComptaLineSup)
        {
        }



        public override void commit()
        {
            base.commit();
            this._idLine = this.idLine;
            this._idAccount = this.IdAccount;
        }

        public override void reject()
        {
            base.reject();
            this.idLine = this._idLine;
            this.IdAccount = this._idAccount;
        }
    }
    
}
